package com.koudai.finance.utils;

import java.math.BigDecimal;

/**
 * @Description: 金额转换
 * @Author: ls
 * @Date: 2020/08/13 14:00
 */
public class AmountUtils {

    /**
     * 金额为分的格式
     */
    private static final String CURRENCY_FEN_REGEX = "-?[0-9]+";

    /**
     * @Description: 将分为单位的转换为元并返回金额格式的字符串 （除100）
     * @Author: ls
     * @Date: 2020/08/13 14:25
     */
    public static String changeF2Y(Long amount) {
        if (amount == null) {
            return "0";
        }
        if (!amount.toString().matches(CURRENCY_FEN_REGEX)) {
            return "0";
        }
        String resultStr = "";
        int flag = 0;
        String amString = amount.toString();
        if (amString.charAt(0) == '-') {
            flag = 1;
            amString = amString.substring(1);
        }
        StringBuilder result = new StringBuilder();
        if (amString.length() == 1) {
            result.append("0.0").append(amString);
        } else if (amString.length() == 2) {
            result.append("0.").append(amString);
        } else {
            String intString = amString.substring(0, amString.length() - 2);
            for (int i = 1; i <= intString.length(); i++) {
                result.append(intString, intString.length() - i, intString.length() - i + 1);
            }
            result.reverse().append(".").append(amString.substring(amString.length() - 2));
        }
        if (flag == 1) {
            resultStr = "-" + result.toString();
        } else {
            resultStr = result.toString();
        }
        return getPrettyNumber(resultStr);
    }

    /**
     * @Description: 将分为单位的转换为元 （除100）
     * @Author: ls
     * @Date: 2020/08/13 14:23
     */
    public static String changeF2Y(String amount) {
        if (!amount.matches(CURRENCY_FEN_REGEX)) {
            return "0";
        }
        String result = BigDecimal.valueOf(Long.parseLong(amount)).divide(new BigDecimal(100)).toString();
        return getPrettyNumber(result);
    }

    /**
     * @Description: 去掉最后一个0
     * @Author: ls
     * @Date: 2020/08/13 15:13
     */
    public static String getPrettyNumber(String number) {
        if (!number.endsWith("0")) {
            return number;
        }
        return new BigDecimal(number)
                .stripTrailingZeros().toPlainString();
    }

    /**
     * @Description: 将元为单位的转换为分 （乘100）
     * @Author: ls
     * @Date: 2020/08/13 14:22
     */
    public static String changeY2F(Long amount) {
        if (amount == null) {
            return "0";
        }
        return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();
    }

    /**
     * @Description: 将元为单位的转换为分 替换小数点，支持以逗号区分的金额
     * @Author: ls
     * @Date: 2020/08/13 14:21
     */
    public static String changeY2F(String amount) {
        if ("0".equals(amount)) {
            return "0";
        }
        String currency = amount.replaceAll("[$￥,]", "");
        int index = currency.indexOf(".");
        int length = currency.length();
        long amLong;
        if (index == -1) {
            amLong = Long.parseLong(currency + "00");
        } else if (length - index >= 3) {
            amLong = Long.parseLong((currency.substring(0, index + 3)).replace(".", ""));
        } else if (length - index == 2) {
            amLong = Long.parseLong((currency.substring(0, index + 2)).replace(".", "") + 0);
        } else {
            amLong = Long.parseLong((currency.substring(0, index + 1)).replace(".", "") + "00");
        }
        return Long.toString(amLong);
    }

}